#===============================================================================
# Koro Business Environment
# Copyright (C) 2007,2008,2010  Antonio Corroppoli
#
#This file is part of Koro BE.
# 
# Koro BE is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
# 
# Koro BE is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with Koro BE; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
#===============================================================================

print 'importo guifacade'
from koro.ui.wxgui import korogui
#from koro.ui.wxgui import pages



### In teoria non c'e' bisogno di self.__app visto che c'e' self.__main_frame
### self.__app ora viene usato solo per ottenere MainFrame

class GuiFacade:
    __instance = None
    @staticmethod
    def get_instance():        
        return (GuiFacade.__instance or GuiFacade())
        
    def __init__(self):        
        assert GuiFacade.__instance is None
#        import pdb
#        pdb.set_trace()
        GuiFacade.__instance = self
        self.__app = None
        self.__main_frame=None
    
#    def render(self,context,data):
#        print 'guifacade.render'
#        assert isinstance(data,list),Exception("guifacade.render: Atteso tipo lista")
#        assert isinstance(context,type),Exception("guifacade.render: Atteso tipo type")
#        page=map.page_by(context)        
#        self.__main_frame.show_page(page,context,data)

    def get_options_page_ID(self):
        return korogui.ID_OPZIONI_PAGE
    
    def get_option_page(self):
        import koro.ui.wxgui.opzioni
        return koro.ui.wxgui.opzioni.get_opzioni_page()
    
    def request_view(self,page,data,context,*args,**kwargs):
        print 'guifacade.request_view'
        assert isinstance(data,list),Exception("guifacade.render: Atteso tipo lista")
        assert isinstance(context,type),Exception("guifacade.render: Atteso tipo type")
        self.__main_frame.show_page(page,data,context,*args,**kwargs)
    
    def show_home_page(self):
        self.__main_frame._set_home_page()
                

#    def viewClientiPage(self,data):        
#        page=pages.ClientiPage(self.__app.get_main_frame(),data)
#        self.__app.get_main_frame().show_page(page)
#    
#    def viewFornitoriPage(self,data):        
#        page=pages.FornitoriPage(self.__app.get_main_frame(),data)
#        self.__app.get_main_frame().show_page(page)
#    
#    def viewProdottiPage(self,data):
#        page=pages.ProdottiPage(self.__app.get_main_frame(),data)
#        self.__app.get_main_frame().show_page(page)        
#        
#    
#    def viewCategorieProdottiPage(self,data):
#        page=pages.CategorieProdottiPage(self.__app.get_main_frame(),data)
#        self.__app.get_main_frame().show_page(page)
#    
#    def viewOrdiniFornitorePage(self,data):
#        page=pages.OrdiniFornitorePage(self.__app.get_main_frame(),data)        
#        self.__app.get_main_frame().show_page(page)
#    
#    def viewListiniPage(self,data):
#        page=pages.ListiniPage(self.__app.get_main_frame(),data)        
#        self.__app.get_main_frame().show_page(page)
#    
#    def viewCodiciIvaPage(self,data):
#        page=pages.CodiciIvaPage(self.__app.get_main_frame(),data)        
#        self.__app.get_main_frame().show_page(page)
    #nuovo metodo
    def main_loop(self):
        print 'koro.ui.wxgui.guifacade.main_loop'
        #self.__app.Destroy()
        
        #se questa chiamata va in errore
        #i medodi che usano self.__app non funzionano
        #self.__app = korogui.KoroGui(observer,False)
        self.__app.show()
#        self.__main_frame=self.__app.get_main_frame()        
        self.__app.MainLoop()
    
    def configure(self,observer,configuration):
        #self.__app=korogui.KoroGui(observer,False)
        self.__main_frame=self.__app.get_main_frame()
        self.__main_frame.do_menu_bar(configuration)
    
    def show_message(self,msg):
        self.__app.show_message(msg)
    
    def show_error(self,msg):            
        self.__app.show_error(msg)
    
    def init(self,observer):
        self.__app=korogui.KoroGui(observer,False)

#    def showError(self,errMsg):
#        """rivedere"""
#        if self.__app:
#            self.__app.show_message(errMsg,mell.mMSG_ERROR)
#        else:
#            import wx
###            app=wx.PySimpleApp()
#            msgD = wx.MessageDialog(None,str(errMsg),style=wx.OK|mell.mMSG_ERROR)
#            msgD.ShowModal()
#            msgD.Destroy()
#            print errMsg
#
#    def show_message(self,msg):
#        if self.__app:
#            self.__app.show_message(msg)
#        else:
#            import wx
###            app=wx.PySimpleApp()
#            msgD = wx.MessageDialog(None,str(msg),style=wx.OK|mell.mMSG_INFO)
#            msgD.ShowModal()
#            msgD.Destroy()
#            print msg
#        
#
#    def uiData(self,data):
#        return uiDataList(data)


#class TestFacade:
#    __instance = None
#    @staticmethod
#    def get_instance():        
#        return (TestFacade.__instance or TestFacade())
#        
#    def __init__(self):        
#        assert TestFacade.__instance is None
#        TestFacade.__instance = self
#        self.__app = None        
#
#    def userLogin(self):
#        return
#        assert self.__app is None
#        self.__app = mell.LoginApp(0)
#        return self.__app.login()
#
#    def test(self,handler):
#        #assert isinstance(self.__app,mell.LoginApp),"Chiamare prima userLogin()"
#        #self.__app.Destroy()
#        self.__app = mell.TestApp(0)
#        self.setHandler(handler)
#        self.__app.MainLoop()
#
#    def showError(self,errMsg):
#        self.__app.show_message(errMsg,mell.mMSG_ERROR)
#
#    def show_message(self,msg):
#        self.__app.show_message(msg)
#
#    def setHandler(self,handler):
#        self.__app.mGetHandlerManager().mSetEventHandler(handler)
#
#    def uiData(self,data):
#        return uiDataList(data)
#        

